<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>回文检测器</title>
    </title>
</head>

<body>
    <script>
        class Deque {
            constructor() {
                this.item = [];
            }
            addFront(element) { //前端插入
                this.item.unshift(element);
            }
            addBack(element) { //后端插入
                this.item.push(element);
            }
            removeFront() { //前端删除
                return this.item.shift();
            }
            removeBack() { //后端删除
                return this.item.pop();
            }
            peekFront() { //前端返回
                if (this.item.length === 0) {
                    return undefined;
                } else {
                    return this.item[0];
                }
            }
            peekBack() { //后端返回
                if (this.item.length === 0) {
                    return undefined;
                } else {
                    return this.item[this.item.length - 1];
                }
            }
            toString() {
                return this.item.toString();
            }
            isEmpty() {
                // return this.item.length === 0;
                return this.size() === 0;
            }
            size() {
                return this.item.length;
            }
        }


        function palindromeChecker(aString) {

            // 1.保证有字符串
            if (aString === undefined || aString === null || (aString != null && aString.length === 0)) {
                return false;
            }
            // 2.对字符串预处理   保证大小写统一，去掉空格
            const lowerString = aString.toLowerCase().split(" ").join("");

            // return lowerString;

            // 3.字符串放到双端队列
            const deque = new Deque();
            for (let i = 0; i < lowerString.length; i++) {
                deque.addBack(lowerString.charAt(i));
            }

            // return deque;

            // 判断是否为回文
            let isEqual = true;
            let firstCase, lastCase;
            while (deque.size() > 1 && isEqual) {
                firstCase = deque.removeFront();
                lastCase = deque.removeBack();
                if (firstCase !== lastCase) {
                    isEqual = false;
                }
            }
            return isEqual;

        }


        let str = 'A bccba';
        // console.log("deque:" + palindromeChecker(str));
        console.log(str);
        console.log("回文:" + palindromeChecker(str));
    </script>
</body>

</html>